// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/ScheduleModelApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/base/model/ScheduleModelListQueryReq'
import OneClass from '@api/base/model/ScheduleModelReq'
import AddClass from '@api/base/model/ScheduleModelOperateReq'
import PassClass from '@api/base/model/ScheduleModelBomReq'
import BaseClass from '@api/base/model/ScheduleModelDto'
import UserClass from '@api/base/model/ModelMemberDto'
import ChildClass from '@api/base/model/ModelContentDto'

const EditClass = AddClass
const DelClass = OneClass
const RejClass = PassClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const USER = [UserClass, 'member', 1]
const STATE = [ChildClass, 'state', 1]
const RECORD = [ChildClass, 'record', 1]
const EVENT = [ChildClass, 'event', 1]
const BASE = [BaseClass, 'content', null, (dto, obj) => CORE.grpcDTO(dto, obj, USER, STATE, RECORD, EVENT)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryScheduleModelList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { modelId: id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryScheduleModelDetail', DTO)
    return resp.content || {}
  },
  transOne(obj, { state: { MAP } }) {
    obj.userIds = obj.member.map((item) => item.id).join()
    obj.state.forEach((item) => {
      item.key = item.id
      obj[`state_${item.id}`] = MAP.checkState(item)
    })
    obj.record.forEach((item) => {
      item.key = item.id
      obj[`record_${item.id}`] = MAP.checkRecord(item)
      obj[`device_${item.id}`] = MAP.checkDevice(item)
      obj[`point_${item.id}`] = MAP.checkPoint(item)
    })
    obj.event.forEach((item) => {
      item.key = item.id
      obj[`event_${item.id}`] = MAP.checkEvent(item)
    })
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertScheduleModel', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateScheduleModel', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { modelId: id })
    await CORE.grpc(TIP, CLIENT, 'deleteScheduleModel', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  async pass({ id }, TIP = '启用') {
    const DTO = CORE.grpcDTO(PassClass, { modelId: id, status: 1 })
    await CORE.grpc(TIP, CLIENT, 'bomScheduleModel', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  async rej({ id }, TIP = '禁用') {
    const DTO = CORE.grpcDTO(RejClass, { modelId: id, status: 2 })
    await CORE.grpc(TIP, CLIENT, 'bomScheduleModel', DTO)
    return CORE.grpcOK(TIP)
  },
}
